'use strict'

//const { data } = require("jquery");

class userinfo {
  constructor() {
    this.param = '/'
  }

  async post(c) {
    let {user_head,user_name,user_self,user_id} = JSON.parse(c.body)
    let a = c.service.fun;

    c.setHeader('content-type', 'text/html; charset=utf-8')
      
    let data1 = await a.sel('user_list','',[],c.service.pool);
    let aa =data1.rows.some(item=>{
      return user_id == item.user_id
    })
    //let data1 = a.sel('user_list',{'user_id':`${user_id}`},['user_head','user_name'],c.service.pool);
    if(aa){ 
      let data2 =await a.sel('user_list',{'user_id':`${user_id}`},['user_head','user_name'],c.service.pool);
      if(data2.rows[0].user_head!=user_head){
        a.upd('user_list',{'user_id':`${user_id}`},{'user_head':`${user_head}`},c.service.pool)
        c.send({
          type:'success',
          data:'exist'
        })
      }
      else if(data2.rows[0].user_name != user_name){
        a.upd('user_list',{'user_id':`${user_id}`},{'user_name':`${user_name}`},c.service.pool)
        c.send({
          type:'success',
          data:'exist'
        })
      }
      else{
        c.send({
          type:'success',
          data:'exist'
        })
      }
    }else{
      a.add('user_list',{'user_head':user_head,'user_name':user_name,'user_self':user_self,'user_id':user_id},c.service.pool);
      c.send({
        type:'success',
        data:'ok'
      })
      
    }
      
      
      
  }
}

module.exports = userinfo;

